<?php
namespace TwinPeaks\Core;

abstract class CAction
{
	/**
	 * @var
	 */
	private $ctrlInstance;
	
	/**
	 * @var CDataAdapter
	 */
	protected $data;

	/**
	 * @var array
	 */
	private $errors;
	
	/**
	 * 
	 */
	final function __construct($ctrlInstance, $data)
	{
		$this->ctrlInstance = $ctrlInstance;
		$this->data = $data;
	}
	
	/**
	 * @return void
	 */
	abstract function coerce(CDataAdapter $data);
	
	/**
	 * @return mixed
	 */
	abstract function action();
	
	/**
	 * 
	 */
	static function feed()
	{
		return new static();
	}
	
	/**
	 * @return mixed
	 */
	final function perform()
	{
		if ($this->hasErrors())
			return;
		
		return $this->action();
	}
	
	/**
	 * @return
	 */
	protected function addError($errId)
	{
		$this->errors[$errId] = true;
	}
	
	/**
	 * @return boolean
	 */
	final function hasErrors()
	{
		return (boolean) count($this->errors);
	}

}
